using System;
using System.IO;
using System.Collections.Generic;

namespace ODSDataExtraction.DefaultExtractors
{
	internal class PlainTextExtractor : DataExtractorBase
	{
		private static readonly string [] supportedExtensions = {".txt", ".log", ".ini"};
		private static readonly string fileContentsKey = "FileContent";
		public PlainTextExtractor ()
		{
		}

		#region IDataExtractor implementation
		public override void ExtractData (FileInfo file, IDictionary<string, string> data)
		{
			bool shouldSkip = true;
			foreach(string ext in supportedExtensions)
			{
				if(ext.Equals(file.Extension, StringComparison.OrdinalIgnoreCase))
				{
					shouldSkip = false;
					break;
				}
			}
			
			if(!shouldSkip)
			{
				if(logger.IsDebugEnabled)
					logger.DebugFormat("Extracting plain text from {0}", file.FullName);
				try
				{
					StreamReader reader = new StreamReader(file.FullName);
					if(!data.ContainsKey(fileContentsKey))
						data.Add(fileContentsKey,reader.ReadToEnd());
				}
				catch(IOException exc)
				{
					logger.ErrorFormat("{0} was unable to extract plain text from {1}. Error: {2}", this.GetType(), file, exc.Message);
				}
			}
			
		}
		#endregion
}
}

